package com.ruoyi.system.utils;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.core.domain.entity.SysMajor;
import com.ruoyi.common.core.domain.entity.SysMajorCate;

/**
 * @author wangxianl
 * createTime: 2023/7/22.
 * description: 行业判断
 */
public class MajorUtils {

    /**
     * 根据行业代码获取专业
     */
    public static SysMajor getMajorByHydm(String hydm) {
        if (ReUtil.isMatch("^(0[1-5]\\d*)", hydm)) {
            return new SysMajor().setName("农业").setCode("A");
        }
        if (ReUtil.isMatch("^(0[6-9]|[1-3][0-9]|4[0-6])\\d*", hydm)) {
            return new SysMajor().setName("工业").setCode("B");
        }
        if (ReUtil.isMatch("^(4[7-9]|50)\\d*", hydm)) {
            return new SysMajor().setName("建筑业").setCode("C");
        }
        if (ReUtil.isMatch("^(5[12])\\d*", hydm)) {
            return new SysMajor().setName("批发零售业").setCode("E");
        }
        if (ReUtil.isMatch("^(6[12])\\d*", hydm)) {
            return new SysMajor().setName("住宿餐饮业").setCode("S");
        }
        if (ReUtil.isMatch("^(5[3-9]|6[03456789]|7[1-9]|8|9[0-6]|70[2-9])\\d*", hydm)) {
            return new SysMajor().setName("服务业").setCode("F");
        }
        if (ReUtil.isMatch("^701\\d*", hydm)) {
            return new SysMajor().setName("房地产业").setCode("X");
        }
        return null;
    }

    /**
     * 根据行业代码获取专业代码
     */
    public static String getMajorCodeByHydm(String hydm) {
        SysMajor major = getMajorByHydm(hydm);
        if (major != null) {
            return major.getCode();
        }
        return null;
    }

    /**
     * 根据行业代码获取行业门类
     */
    public static SysMajorCate getMajorCateByHydm(String hydm) {
        if (StrUtil.isEmpty(hydm) || hydm.length() < 2) {
            return null;
        }
        int dmValue = Integer.parseInt(hydm.substring(0, 2));
        String[][] cateList = new String[][]{
                new String[]{"农、林、牧、渔业", "A", "01", "05" },
                new String[]{"采矿业", "B", "06", "12" },
                new String[]{"制造业", "C", "13", "43" },
                new String[]{"电力、热力、燃气及水生产和供应业", "D", "44", "46" },
                new String[]{"建筑业", "E", "47", "50" },
                new String[]{"批发和零售业", "F", "51", "52" },
                new String[]{"交通运输、仓储和邮政业", "G", "53", "60" },
                new String[]{"住宿和餐饮业", "H", "61", "62" },
                new String[]{"信息传输、软件和信息技术服务业", "I", "63", "65" },
                new String[]{"金融业", "J", "66", "69" },
                new String[]{"房地产业", "K", "70", "70" },
                new String[]{"租赁和商务服务业", "L", "71", "72" },
                new String[]{"科学研究和技术服务业", "M", "73", "75" },
                new String[]{"水利、环境和公共设施管理业", "N", "76", "79" },
                new String[]{"居民服务、修理和其他服务业", "O", "80", "82" },
                new String[]{"教育", "P", "83", "83" },
                new String[]{"卫生和社会工作", "Q", "84", "85" },
                new String[]{"文化、体育和娱乐业", "R", "86", "90" },
                new String[]{"公共管理、社会保障和社会组织", "S", "91", "96" },
                new String[]{"国际组织", "T", "97", "97" }
        };
        //循环判断行业
        for (String[] cate : cateList) {
            int minCode = Integer.parseInt(cate[2]);
            int maxCode = Integer.parseInt(cate[3]);
            if (dmValue >= minCode && dmValue <= maxCode) {
                return new SysMajorCate(cate[0], cate[1], cate[2], cate[3]);
            }
        }
        return null;
    }

    /**
     * 根据行业代码获取行业门类代码
     */
    public static String getMajorCateCodeByHydm(String hydm) {
        SysMajorCate majorCate = getMajorCateByHydm(hydm);
        if(majorCate!=null){
            return majorCate.getCode();
        }
        return null;
    }
}
